/******************************************
 *Author:庄育龙
 *Function:设置对话框读取、存储设置的实现
 ******************************************/

#include "dialog.h"
#include "ui_dialog.h"
#include <QSettings>
#include <QTextCodec>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
  //Dialog的构造函数
{
    ui->setupUi(this);

    setfile = QApplication::applicationDirPath()+"/settings.ini";
    QSettings settings(setfile, QSettings::IniFormat);
    QByteArray nameBytes=settings.value("Name/Name").toByteArray();
    QByteArray nameBytes2=settings.value("Name/Name2").toByteArray();
    QTextCodec * codec=QTextCodec::codecForName("UTF8");
    QString name=codec->toUnicode(nameBytes);
    QString name2=codec->toUnicode(nameBytes2);
    ui->lineEdit->setText(name);
    ui->lineEdit_2->setText(name2);
    //以上几行与mainwindow.cpp中的on_action_4_triggered()槽功能相似，从setttings.ini中读取设置
    if(settings.value("First/Human").toInt()) {
        ui->radioButton->setChecked(1);
    }
    else {
        ui->radioButton_2->setChecked(1);
    }
    if(settings.value("Forbidden/Enable").toInt()) {
        ui->checkBox->setChecked(1);
    }
    else {
        ui->checkBox->setChecked(0);
    }
    if(settings.value("Difficulty/Difficulty").toInt()==1) {
        ui->radioButton_3->setChecked(1);
    }
    else if(settings.value("Difficulty/Difficulty").toInt()==2) {
        ui->radioButton_4->setChecked(1);
    }
    else {
        ui->radioButton_5->setChecked(1);
    }
    //通过读取设置，赋好相应的值，并初始化设置界面
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_buttonBox_accepted()//本函数在点击确定后把设置保存在设置文件中，供其他部分调用，也能保证在下一次开启程序时仍然是这个设置
{
    QSettings settings(setfile, QSettings::IniFormat);
    QString name=ui->lineEdit->text();
    QString name2=ui->lineEdit_2->text();
    if(ui->radioButton->isChecked()) {
        settings.setValue("First/Human",1);
    }
    else if(ui->radioButton_2->isChecked()) {
        settings.setValue("First/Human",0);
    }
    else{

    }
    if(ui->radioButton_3->isChecked()) {
        settings.setValue("Difficulty/Difficulty",1);
    }
    else if(ui->radioButton_4->isChecked()) {
        settings.setValue("Difficulty/Difficulty",2);
    }
    else if(ui->radioButton_5->isChecked()) {
        settings.setValue("Difficulty/Difficulty",3);
    }
    else {

    }
    if(ui->checkBox->isChecked()) {
        settings.setValue("Forbidden/Enable",1);
    }
    else {
        settings.setValue("Forbidden/Enable",0);
    }
    settings.setValue("Name/Name",name.toUtf8().data());
    settings.setValue("Name/Name2",name2.toUtf8().data());

}

void Dialog::on_radioButton_5_toggled(bool checked)
//因为调用困难AI是直接调用另一个进程，故无法开启禁手选项，如果选择了困难AI而且让电脑先手那么就禁用禁手选项
{
    if(checked && ui->radioButton_2->isChecked()) {
        ui->checkBox->setChecked(0);
        ui->checkBox->setEnabled(0);
    }
    else {
        ui->checkBox->setEnabled(1);
    }
}

void Dialog::on_radioButton_2_toggled(bool checked)
//同上
{
    if(checked && ui->radioButton_5->isChecked()) {
        ui->checkBox->setChecked(0);
        ui->checkBox->setEnabled(0);
    }
    else {
        ui->checkBox->setEnabled(1);
    }
}
